package com.bw.car_module.carlist;

import com.bw.car_module.carlist.dao.CartDao;
import com.bw.common.mvvm.model.BaseRespository;
import com.bw.network_sdk.BaseEntity;
import com.bw.network_sdk.HttpType;
import com.bw.network_sdk.NetWorkFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.Maybe;
import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.Observer;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Function;

/**
 * 购物车Respository
 * */
public class CartRespository extends BaseRespository {

    //获取购物车列表接口
    public Observable<BaseEntity> requestCartList(Map<String,Object>map){
        Observable<CartEntity>cartEntityObservable =
                NetWorkFactory.factory(HttpType.TOKENSIGN)
                        .create(ApiService.class)
                        .requestSelCart(createBody(map));
        return checkNetWork(cartEntityObservable);
    }
    //添加一条购物车数据
    public Observable<BaseEntity> requestInsertCart(Map<String,Object>map){
        Observable<AddCartEntity>addCartEntityObservable =
                NetWorkFactory.factory(HttpType.TOKEN)
                        .create(ApiService.class)
                        .requestAddCart(createBody(map));
        return checkNetWork(addCartEntityObservable);
    }
    //删除一条购物车数据
    public Observable<BaseEntity>requestDelCart(Map<String,Object>map){
        Observable<DelCartEntity>delCartEntityObservable =
                NetWorkFactory.factory(HttpType.TOKEN)
                        .create(ApiService.class)
                        .requestDelCart(createBody(map));
        return checkNetWork(delCartEntityObservable);
    }
    //提交购物车数据
    public Observable<BaseEntity>requestSubmitCart(Map<String,Object>map){
        Observable<SubmitCartEntity>submitCartEntityObservable =
                NetWorkFactory.factory(HttpType.TOKEN)
                        .create(ApiService.class)
                        .requestSubmitCart(createBody(map));
        return checkNetWork(submitCartEntityObservable);
    }
    /**
     * 修改购物车数据
     * cartIdList:List<Long></>
     * */
    public Observable<BaseEntity>requestUpdateCart(Map<String,Object>map){
        Map<String,Object>delMap = new HashMap<>();
        List<Long>ids = (List<Long>) map.get("cartIdList");
        if (ids==null){
            List<CartValueEntity>addCartList = (List<CartValueEntity>) map.get("addCartList");
            List<Observable<BaseEntity>>addCartObservable = new ArrayList<>();
            for (CartValueEntity valueEntity : addCartList){
                Map<String,Object>addMap = new HashMap<>();
                addMap.put("goodsCount",valueEntity.goods_count);
                addMap.put("goodsDesc",valueEntity.goods_desc);
                addMap.put("goodsIcon",valueEntity.goods_icon);
                addMap.put("goodsId",valueEntity.goods_id);
                addMap.put("goodsPrice",valueEntity.goods_price);
                addMap.put("goodsSku",valueEntity.goods_sku);
                addMap.put("time",valueEntity.time);
                addMap.put("userId",valueEntity.user_id);
                addCartObservable.add(requestInsertCart(addMap));
            }
            return Observable.merge(addCartObservable);
        }
        delMap.put("cartIdList",ids);
        map.remove("cartIdList");
        //删除购物车列表数据被观察者
        Observable<BaseEntity>delObservable = requestDelCart(delMap);
        //循环配置添加购物车请求得被观察者
        List<CartValueEntity>addCartList = (List<CartValueEntity>) map.get("addCartList");
        List<Observable<BaseEntity>>addCartObservable = new ArrayList<>();
        for (CartValueEntity valueEntity : addCartList){
            Map<String,Object>addMap = new HashMap<>();
            addMap.put("goodsCount",valueEntity.goods_count);
            addMap.put("goodsDesc",valueEntity.goods_desc);
            addMap.put("goodsIcon",valueEntity.goods_icon);
            addMap.put("goodsId",valueEntity.goods_id);
            addMap.put("goodsPrice",valueEntity.goods_price);
            addMap.put("goodsSku",valueEntity.goods_sku);
            addMap.put("time",valueEntity.time);
            addMap.put("userId",valueEntity.user_id);
            addCartObservable.add(requestInsertCart(addMap));
        }
        addCartObservable.add(0,delObservable);
        return Observable.merge(addCartObservable);
    }

    //离线购物车操作
    //离线购物车修改
    public Observable<BaseEntity>updateCart(List<CartValueEntity>list, CartDao dao){
        Observable<OffLineUpdateEntity>offLineUpdateEntityObservable =
                new Observable<OffLineUpdateEntity>() {
                    @Override
                    protected void subscribeActual(Observer<? super OffLineUpdateEntity> observer) {
                        for (CartValueEntity cartValueEntity : list){
                            dao.updateCart(cartValueEntity);
                        }
                        OffLineUpdateEntity entity = new OffLineUpdateEntity();
                        entity.status = 200;
                        entity.message = "离线购物车更新成功";
                        observer.onNext(entity);
                        observer.onComplete();
                    }
                };
        return functionObservable(offLineUpdateEntityObservable);
    }
    //清空离线数据源
    public Observable<BaseEntity>delCart(CartDao dao){
        Observable<OffLineDelEntity>offLineDelEntityObservable =
                new Observable<OffLineDelEntity>() {
                    @Override
                    protected void subscribeActual(Observer<? super OffLineDelEntity> observer) {
                        dao.delAllCart();
                        OffLineDelEntity offLineDelEntity = new OffLineDelEntity();
                        offLineDelEntity.status = 200;
                        offLineDelEntity.message = "清空离线购物车成功";
                        observer.onNext(offLineDelEntity);
                        observer.onComplete();
                    }
                };
        return functionObservable(offLineDelEntityObservable);
    }
    //删除数据库中某些数据
    public Observable<BaseEntity>delCart(List<CartValueEntity>list, CartDao dao){
        Observable<OffLineDelEntity>offLineDelEntityObservable =
                new Observable<OffLineDelEntity>() {
                    @Override
                    protected void subscribeActual(Observer<? super OffLineDelEntity> observer) {
                        for (CartValueEntity entity : list){
                            dao.delCart(entity);
                        }
                        OffLineDelEntity offLineDelEntity = new OffLineDelEntity();
                        offLineDelEntity.status = 200;
                        offLineDelEntity.message = "本地数据删除成功";
                        observer.onNext(offLineDelEntity);
                        observer.onComplete();
                    }
                };
        return functionObservable(offLineDelEntityObservable);
    }
    //查询离线购物车数据
    public Observable<BaseEntity>selCart(CartDao dao){
        Maybe<List<CartValueEntity>>selMaybe = dao.selCart();
        Observable<OffLineCartEntity> observable = selMaybe.flatMapObservable(new Function<List<CartValueEntity>,
                ObservableSource<OffLineCartEntity>>() {
            @Override
            public ObservableSource<OffLineCartEntity> apply(
                    @NonNull List<CartValueEntity> list) throws Exception {
                OffLineCartEntity offLineCartEntity = new OffLineCartEntity();
                offLineCartEntity.status = 200;
                offLineCartEntity.message = "查询成功";
                offLineCartEntity.data = list;
                return Observable.just(offLineCartEntity);
            }
        });
        return functionObservable(observable);
    }
    //添加离线购物车数据
    public Observable<BaseEntity>insertCart(Map<String,Object>map,CartDao dao){
        CartValueEntity valueEntity = new CartValueEntity();
        valueEntity.goods_count = (int) map.get("goodsCount");
        valueEntity.goods_desc = (String) map.get("goodsDesc");
        valueEntity.goods_icon = (String) map.get("goodsIcon");
        valueEntity.goods_id = (long) map.get("goodsId");
        valueEntity.goods_price = (String) map.get("goodsPrice");
        valueEntity.goods_sku = (String) map.get("goodsSku");
        valueEntity.time = System.currentTimeMillis();
        valueEntity.user_id = 0;
        Observable<OffLineInsertEntity>offLineInsertEntityObservable =
                new Observable<OffLineInsertEntity>() {
                    @Override
                    protected void subscribeActual(Observer<? super OffLineInsertEntity> observer) {
                        dao.insertCart(valueEntity);
                        OffLineInsertEntity entity = new OffLineInsertEntity();
                        entity.status = 200;
                        entity.message = "插入成功";
                        observer.onNext(entity);
                        observer.onComplete();
                    }
                };
        return functionObservable(offLineInsertEntityObservable);
    }
}
